from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import *


class CustomUserAdmin(UserAdmin):
    # 指定在列表页显示的字段
    list_display = ('id','uid', 'username', 'email', 'phone', 'register_time','avatar_key')

    # 指定在编辑页显示的字段
    fieldsets = (
        (None, {'fields': ('username', 'password')}),
        ('个人信息', {'fields': ('uid', 'email', 'phone')}),
        ('权限', {'fields': ('is_active', 'is_staff', 'is_superuser', 'groups', 'user_permissions')}),
    )

    # 添加用户时的字段
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('username', 'password1', 'password2', 'email', 'phone'),
        }),
    )

    # 可搜索的字段
    search_fields = ('username', 'email', 'phone', 'uid')

    # 可过滤的字段
    list_filter = ('is_staff', 'is_superuser', 'is_active', 'groups')

    # 排序字段
    ordering = ('username',)

    def get_queryset(self, request):
        # 默认显示非管理员用户
        qs = super().get_queryset(request)
        return qs.filter(is_superuser=False)
# 方法1：使用register装饰器（推荐）
@admin.register(Address)
class AddressAdmin(admin.ModelAdmin):
    list_display = ('id','receiver_name', 'receiver_phone', 'detail','user', 'created_at','is_default')

# 注册自定义用户模型和管理类
admin.site.register(CustomUser, CustomUserAdmin)